package com.aistar.user.controller;


import com.aistar.common.vo.Result;
import com.aistar.common.vo.StatusCode;
import com.aistar.user.entity.User;
import com.aistar.user.service.IUserService;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by js on 2019/6/27.
 */
@RestController
@Slf4j
public class ProfileController {
    @Autowired
    private IUserService iUserService;


    @GetMapping("/profile")
    public Result findUser(HttpServletRequest request){
        Claims claims= (Claims) request.getAttribute("claims");

        if (null==claims){
            return new Result(false,StatusCode.LOGINERROR,"请先登录");
        }else {
            //获取用户信息
            String id=claims.getId();
            System.out.println(id);
            User user=iUserService.findById(id);


            return new Result(true,StatusCode.OK,"查询成功",user);
        }

    }



    @PutMapping("/profile")
    public Result update(User user){
        Result result=null;
        System.out.println(user);

        iUserService.update(user);
        result=new Result(true,StatusCode.OK,"更新成功");
        return result;
    }

    @RequestMapping("/upload")
    public Result upload(MultipartFile file,HttpServletRequest request){
        String path=request.getServletContext().getRealPath("/");
        System.out.println(path);;
        System.out.println(file.getOriginalFilename());
        return new Result(true,StatusCode.OK,"ok");
    }

}
